<template>
    <div class="con">


        <div class="main">
            <p>{{state.storeName}}</p>

            <div class="num">
                <p>
                    <span>¥</span>{{state.totalAmount}}</p>
            </div>

        </div>

        <div class="btn" @click="ok()" :class="{ graybtn: isgraybtn }">
            确认支付
        </div>

    </div>
</template>

<script>
export default {
    name: "transfer",
    data() {
        return {
            auth_code: "",
            isgraybtn: false,
            state: {},
            payid:""
        };
    },
    methods: {
        ok() {
            this.isgraybtn = true;

            var timestamp = Date.parse(new Date()) + 100;

            // window.location.href = `https://api.yintu168.com:8866/PABC/pay/getBuyerId.do?appid=web.2018&format=json&timestamp=${timestamp}&authCode=${
            //     this.auth_code
            // }&vendorId=${this.state.vendorId}&merTradeNo=${
            //     this.state.merTradeNo
            // }`;

            this.axios({
                method: "get",
                url: `${
                    this.siteUrl
                }/alipay/createTrade.do?appid=web.2018&format=json&timestamp=${timestamp}&authCode=${
                    this.auth_code
                }&vendorId=${this.state.vendorId}&merTradeNo=${
                    this.state.merTradeNo
                }&payid=${this.payid}`
                // data: {
                //     authCode: this.auth_code,
                //     vendorId: this.state.vendorId,
                //     merTradeNo: this.state.merTradeNo
                // }
            })
                .then(response => {
                    console.log(response);
                    if (response.data.success == 1) {
                        ap.tradePay(
                            {
                                tradeNO: response.data.data.tradeNo
                            },
                            function(res) {
                                if (res.resultCode === "9000") {
                                    // ap.alert("交易成功");
                                    // AlipayJSBridge.call("vibrate"),
                                    //     resolve(res);
                                    window.location.href="http://yintu.chaojidai.top"
                                } else if (res.resultCode === "6001") {
                                    // 用户取消支付
                                    // ap.alert("取消交易");
                                    window.location.href="https://www.baidu.com"
                                } else {
                                    ap.alert("交易失败请联系管理员");
                                    window.location.href="https://www.baidu.com"
                                    // 第三方支付失败回调
                                    // 错误弹窗
                                    // alert(JSON.stringify(res)), reject(res);
                                }
                            }
                        );
                        // window.location.href = "" + response.data.data.url + "";
                        this.isgraybtn = false;
                    } else {
                        this.$Message.warning(response.data.message);
                        this.isgraybtn = false;
                    }
                })
                .catch(function(error) {
                    console.log(error);
                });
        }
    },
    mounted() {
        var href = window.location.href.split("auth_code=");
        // console.log(href);
        //从url中截取32位的auth_code
        this.auth_code = href[1].substring(0, 32);

        this.payid = this.$route.query.payid;


        this.state = JSON.parse(
            this.$route.query.state.replace(new RegExp("\\\\", "g"), "")
        );

        // this.axios({
        //     method: "get",
        //     url: `${
        //         this.siteUrl
        //     }/alipay/checkTrade.do?appid=web.2018&payid=${this.payid}`
        // })
        //     .then(response => {
        //         console.log(response);
        //         if (response.data.success == 1) {

        //         } else {
                  
        //             window.location.href="https://www.baidu.com"
        //             // console.log("1")
        //         }
        //     })
        //     .catch(function(error) {
        //         console.log(error);
        //     });

      
         
       
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.btn {
    background: #0b94de;
    width: 100%;
    height: 64px;
    line-height: 64px;
    text-align: center;
    color: #fff;
    font-size: 18px;
    position: fixed;
    bottom: 0px;
    left: 0px;
}

.graybtn {
    opacity: 0.5;
}

.con .main {
    width: 90%;
    background: #fff;
    height: 400px;
    margin: 15px auto;
    border-radius: 4px;
    text-align: center;
}
.con .main p {
    margin-top: 30px;
    margin-bottom: 10px;
    font-size: 16px;
}
.con .main .num {
    height: 85px;
    line-height: 85px;
    width: 100%;
    text-align: center;
    border-bottom: 1px dashed #b8d6e6;
}

.con .main .num p {
    font-size: 34px;
    position: relative;
    display: inline;
}
.con .main .num span {
    font-size: 18px;
    position: absolute;
    left: -12px;
}
</style>
